Skip to content

Add Thomas Algorithm in Rust #693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

fanninpm
Copy link
Contributor

@fanninpm fanninpm commented May 7, 2020

For leios: This one is implemented without any external crates, so you can compile it just with rustc. (Let me know if what I did for pretty-printing on line 37 of the code was too much.)

This one is implemented without any external crates, so you can compile
it just with `rustc`.
@@ -0,0 +1,38 @@
// This implementation is based on the C++ implementation.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably remove this comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With your and @leios's blessing, I would probably go on a crusade to remove all such comments (e.g. "This implementation was written by @strega-nil") throughout the codebase.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I think that's a fine idea.

They were originally implemented as a way of letting readers know that this code could have been written by someone who did not write previous implementations in the same language.

Now that the review process is more rigorous, it doesn't have as much meaning.

Liikt
Liikt previously requested changes May 20, 2020
@@ -0,0 +1,38 @@
// This implementation is based on the C++ implementation.

fn thomas(a: &[f64], b: &[f64], c: &[f64], x: &mut Vec<f64>) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you change the &[f64] to a Vec<f64> you can get rid of the references in the main which would be a bit cleaner.

@@ -0,0 +1,38 @@
// This implementation is based on the C++ implementation.

fn thomas(a: &[f64], b: &[f64], c: &[f64], x: &mut Vec<f64>) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also probably return a Vec<64> instead of mutating the Vec. Makes it a bit more cleaner to read imho.

so in the end i think the best signature would be fn thomas(a: Vec<f64>, b: Vec<f64>, c: Vec<f64>, mut x: Vec<f64>) -> Vec<f64>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we agreed in #510 that we want functions to be pure, even though at least the Thomas algorithm functions are all over the place in that regard. With that in mind, I think the best signature is

fn thomas(a: &[f64], b: &[f64], c: &[f64], d: &[f64]) -> Vec<f64> {

so that you have the efficiency of the function not taking ownership of its arguments, but also being pure.

@berquist berquist added the Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) label May 24, 2020
fanninpm added 2 commits May 24, 2020 12:22
I am a fan of pure functions, so I redid the function so that it returns
a Vec<f64> rather than mutating a slice.
let y = thomas(&a, &b, &c, &x);

y.iter()
.for_each(|i| println!("[{:>19}]", format!("{:18}", format!("{:?}", i))));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question before I override @Liikt and approve. Why is this nested and not just a single println!?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I essentially wanted to pretty-print the result in a certain way. (I want positive values to lead with a space.) If you want, I can get rid of the pretty-printing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this the same?

y.iter().for_each(|i| println!("[{:>19}]", i));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite. The result I was looking for satisfies these guidelines:

  1. The sign should either be a negative or a space.
  2. The decimal point should (ideally) be aligned.

I guess we don't have to pretty-print the result if we don't have to.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand better now. As long as the explanation is in the PR thread like this, it's ok.

@berquist berquist dismissed Liikt’s stale review May 24, 2020 21:17

Going with the implementation style given in #510

@berquist berquist merged commit efcb278 into algorithm-archivists:master May 24, 2020
@fanninpm fanninpm deleted the thomas_algorithm_in_rust branch May 24, 2020 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants